
package fileby.gui.tabs;

import fileby.core.ActionKey;
import fileby.ctrl.tabs.TabItemListener;
import fileby.core.IDisplayableFile;
import fileby.ctrl.tabs.ITabItemListenerProvider;
import fileby.ctrl.actions.IActionProvider;
import fileby.ctrl.transferhandlers.ITransferHandlerProvider;
import fileby.core.TransferHandlerType;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import java.awt.Insets;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.TooManyListenersException;
import javax.swing.Action;
import javax.swing.JLayeredPane;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JPopupMenu.Separator;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

/**
 *
 * @author Oscar
 */

class TabItem extends javax.swing.JLayeredPane {

    private final static EtchedBorder ETCHED_BORDER = new EtchedBorder();
    private final static EmptyBorder EMPTY_BORDER = new EmptyBorder(new Insets(2,2,2,2));

    private IDisplayableFile file;
    private IActionProvider actionProvider;
    private IPropertyBundleProvider bundleProvider;
    private ITransferHandlerProvider transferHandlerProvider;
    private ITabItemListenerProvider listenerProvider;
    private JPopupMenu popup;
    private PropertyBundle bundle;
    private boolean isSelected;


    public TabItem(IDisplayableFile file, ITabItemListenerProvider listenerProvider,
            IActionProvider actionProvider, IPropertyBundleProvider bundleProvider,
            ITransferHandlerProvider transferHandlerProvider) {

        this.actionProvider = actionProvider;
        this.transferHandlerProvider = transferHandlerProvider;
        this.bundleProvider = bundleProvider;
        this.listenerProvider = listenerProvider;
        this.file = file;
        initComponents();
        initCtrl();
    }


    public String getAbsolutePath() {
        return file.getAbsolutePath();
    }

    IDisplayableFile getDisplayableFile(){
        return file;
    }

    public void update(){
        setText(file.getName());
        setIcon(file.isDirectory());
        setSelected(file.isSelected());
        name.setCaretPosition(0);
    }

    private void setIcon(boolean isDirectory) {
        if (isDirectory) {
            if (getAbsolutePath().indexOf("\\") == getAbsolutePath().length() - 1)
                icon.setIcon(bundle.getIcon("deviceIcon.icon"));
            else
                icon.setIcon(bundle.getIcon("folderIcon.icon"));
        }
        else
            icon.setIcon(bundle.getIcon("fileIcon.icon"));
    }

    private void setSelected(boolean isSelected) {
        if (isSelected != this.isSelected) {
            if (isSelected) {
                contentPane.setBackground(bundleProvider.createGlobalBundle().getColor("selection.color"));
                contentPane.setBorder(ETCHED_BORDER);
            } else {
                contentPane.setBackground(bundle.getColor("contentPane.background"));
                contentPane.setBorder(EMPTY_BORDER);
            }
            this.isSelected = isSelected;
        }
        this.isSelected = isSelected;
    }

    private void setText(String s) {
        name.setText(s);
    }

    private void initCtrl() {
        initJPopupMenu();
        TabItemListener l = listenerProvider.create(file, popup);
        glassPane.addMouseListener(l);
        glassPane.addMouseMotionListener(l);
        glassPane.setTransferHandler(transferHandlerProvider.create(TransferHandlerType.ITEM_PANEL));
        TabItemMouseOverListener listener = new TabItemMouseOverListener();
        glassPane.addMouseListener(listener);
        try {
            glassPane.getDropTarget().addDropTargetListener(listener);
        } catch (TooManyListenersException ex) {
            ex.printStackTrace();
        }
    }

    private void initJPopupMenu() {
        popup = new JPopupMenu();
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.OPEN)));
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.OPEN_IN_NEW_TAB)));
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.ADD_TO_FAVORITES)));
        popup.add(new Separator());
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.CUT)));
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.COPY)));
        popup.add(new Separator());
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.DELETE)));
        Action rename = actionProvider.getControl(ActionKey.RENAME);
        popup.add(new JMenuItem(rename));
        rename.putValue(ActionKey.RENAME.toString(), name);
        rename.putValue("File", file);
        rename.putValue("GlassPane", glassPane);
    }

    private void mouseOver(boolean isSelected) {
        if (isSelected) {
            contentPane.setBackground(bundleProvider.createGlobalBundle().getColor("rollover.color"));
            contentPane.setBorder(ETCHED_BORDER);
        }
        else{
            contentPane.setBackground(bundle.getColor("contentPane.background"));
            contentPane.setBorder(EMPTY_BORDER);
        }
    }

    class TabItemMouseOverListener extends MouseAdapter implements DropTargetListener {

        public void dragEnter(DropTargetDragEvent dtde) {
            if (!file.isSelected()) {
                mouseOver(true);
            }
        }

        public void dragExit(DropTargetEvent dte) {
            if (!file.isSelected()) {
                mouseOver(false);
            }
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            if (!file.isSelected()) {
                mouseOver(true);
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            if (!file.isSelected()) {
                mouseOver(false);
            }
        }

        public void dragOver(DropTargetDragEvent dtde) {}
        public void dropActionChanged(DropTargetDragEvent dtde) {}
        public void drop(DropTargetDropEvent dtde) {}
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        glassPane = new javax.swing.JPanel();
        contentPane = new javax.swing.JPanel();
        icon = new javax.swing.JLabel();
        name = new javax.swing.JTextField();

        bundle = bundleProvider.create(this.getClass());
        setMaximumSize(new java.awt.Dimension(200, 65));
        setMinimumSize(new java.awt.Dimension(200, 65));

        glassPane.setMaximumSize(new java.awt.Dimension(200, 65));
        glassPane.setMinimumSize(new java.awt.Dimension(200, 65));
        glassPane.setOpaque(false);

        javax.swing.GroupLayout glassPaneLayout = new javax.swing.GroupLayout(glassPane);
        glassPane.setLayout(glassPaneLayout);
        glassPaneLayout.setHorizontalGroup(
            glassPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        glassPaneLayout.setVerticalGroup(
            glassPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 65, Short.MAX_VALUE)
        );

        contentPane.setBackground(bundle.getColor("contentPane.background")); // NOI18N
        contentPane.setMaximumSize(new java.awt.Dimension(200, 65));
        contentPane.setMinimumSize(new java.awt.Dimension(200, 65));
        contentPane.setPreferredSize(new java.awt.Dimension(200, 65));

        icon.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        name.setEditable(false);
        name.setBorder(null);
        name.setOpaque(false);

        javax.swing.GroupLayout contentPaneLayout = new javax.swing.GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(icon, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(contentPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(glassPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 76, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(contentPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(glassPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        glassPane.setName(file.getAbsolutePath());
        setLayer(contentPane, JLayeredPane.DEFAULT_LAYER);
        setLayer(glassPane, JLayeredPane.DRAG_LAYER);
        contentPane.setBorder(EMPTY_BORDER);
        update();
    }// </editor-fold>
    
    private javax.swing.JPanel contentPane;
    private javax.swing.JPanel glassPane;
    private javax.swing.JLabel icon;
    private javax.swing.JTextField name;
    
}
